package com.kolon.business.main.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.business.main.dao.MainDao;
import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.XmlUtil;

@Controller("com.kolon.business.main.controller.XmlNotiController")
public class XmlNotiController implements SimpleController {
	
	@Resource(name="MainDaoImpl")
	private MainDao mainDao;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value="/business/main/xmlNoti.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		XmlUtil xml = new XmlUtil();
		Document doc = xml.make("SERV_LIST");
		// 파라미터
		HttpSession httpSession = request.getSession();
		String osNm = ComUtil.getMobileOS(request.getHeader("user-agent"));
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		// 테스트 관련 세션 셋팅
		String mdfyDttm = request.getParameter("mdfyDttm");
		userMap.put("mdfyDttm", mdfyDttm);
		userMap.put("servTp", ComUtil.getServTp(osNm));
		// 서비스 목록 정보 조회
		List servList = mainDao.getXmlList(userMap);
		// 엘리먼트 생성
		LinkedHashMap<String, Object> dataList = new LinkedHashMap<String, Object>();
		for (int i=0, size=servList.size(); i<size; i++) {
			LinkedHashMap<String, Object> data = new LinkedHashMap<String, Object>();
			Map map = (Map)servList.get(i);
			data.put("SERV_NO", map.get("SERV_NO"));
			data.put("SERV_NM", map.get("SERV_NM"));
			// 노티 URL 처리
			if (map.get("NOTI_URL") != null && !"".equals(map.get("NOTI_URL"))) {
				// XML DOCUMENT 및 인증 처리
				Document notiDoc = ComUtil.getDocument((String)map.get("NOTI_URL") + (String)userMap.get("userId"), 
						(String)userMap.get("userId"), (String)userMap.get("userPw"));
				Element element = notiDoc.getRootElement();
				List xmlList = element.getChildren("item");
				Iterator itXml = xmlList.iterator();
				// XML 데이터 
				if (itXml.hasNext()) {
					Element el = (Element)itXml.next();
					map = new HashMap();
					data.put("NOTI_CNT", el.getChild("cnt").getText());
				}
			} else {
				data.put("NOTI_CNT", "0");
			}
			if (map.get("CPY_CD") == null)
			dataList.put("COMM_SERV_INFO", data);
			else
			dataList.put("CPY_SERV_INFO", data);
			xml.addElement(doc.getRootElement(), dataList);
		}
		
		Map model = new HashMap();
		model.put("xmlDoc", doc);
		// 리턴
		return new ModelAndView("xmlView", model);
	}

}
